package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Ranks;
import org.example.erp.service.RanksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RanksController {
    @Autowired
    RanksService ranksService;
    @GetMapping("/rank/find")
    public DataResult find(
            @RequestParam(value = "rankNo") String rankNo,
            @RequestParam(value = "rankArrangement") String rankArrangement,
            @RequestParam(value = "rankArrangementName") String rankArrangementName,
            @RequestParam(value = "salaryCap") String salaryCap,
            @RequestParam(value = "salaryMinimum") String salaryMinimum,
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<Ranks> page = Page.of(currentPage, pageSize);
        QueryWrapper<Ranks> queryWrapper = new QueryWrapper();
        if (rankNo != null && !"".equals(rankNo)) {
            queryWrapper.lambda().like(Ranks::getRankNo, rankNo);
        }
        if (rankArrangement != null && !"".equals(rankArrangement)) {
            queryWrapper.lambda().like(Ranks::getRankArrangement, rankArrangement);
        }
        if (rankArrangementName != null && !"".equals(rankArrangementName)) {
            queryWrapper.lambda().like(Ranks::getRankArrangementName, rankArrangementName);
        }
        Integer salaryCapValue = null;
        Integer salaryMinimumValue = null;
        if (salaryCap != null && !"".equals(salaryCap.trim())) {
            salaryCapValue = Integer.parseInt(salaryCap.trim());
            queryWrapper.lambda().ge(Ranks::getSalaryCap, salaryCapValue); // 假设你要查找 salaryCap 大于等于这个值的记录
        }
        if (salaryMinimum != null && !"".equals(salaryMinimum.trim())) {
            salaryMinimumValue = Integer.parseInt(salaryMinimum.trim());
            queryWrapper.lambda().le(Ranks::getSalaryMinimum, salaryMinimumValue); // 假设你要查找 salaryMinimum 小于等于这个值的记录
        }
        page = ranksService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @GetMapping("/rank/findById")
    public DataResult findById(@RequestParam(value = "rankId") String rankId) {
        Ranks ranks = ranksService.getById(rankId);
        return DataResult.success(ranks);
    }
    @PostMapping("/rank/insert")
    public DataResult insert(@RequestBody Ranks ranks) {
        boolean save = ranksService.save(ranks);
        return DataResult.success(save?"成功":"失败");
    }
    @PutMapping("/rank/update")
    public DataResult update(@RequestBody Ranks ranks) {
        boolean b = ranksService.updateById(ranks);
        return DataResult.success(b?"成功":"失败");
    }
    @DeleteMapping("/rank/delete")
    public DataResult delete(@RequestBody List<String> rankId) {
        boolean b = ranksService.removeByIds(rankId);
        return DataResult.success(b?"成功":"失败");
    }
}
